package com.initialize.gateway.filter;


import com.initialize.commom.config.header.HeaderConfig;
import com.initialize.common.tool.util.RandomTool;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 用于内部的服务转发
 */
@Slf4j
@Component
public class AuthAndLogFilter implements GlobalFilter, Ordered {


    @Autowired
    com.initialize.common.util.redis.RedisUtil redisUtil;


    @SneakyThrows
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //添加一个通信令牌
        String uuidNo_ = RandomTool.createUuidNo_();
        redisUtil.sSet(HeaderConfig.INTERNAL_SECRET_KEY, uuidNo_);
        ServerHttpRequest req = exchange.getRequest().mutate()
                .header(HeaderConfig.INTERNAL_SECRET_KEY, uuidNo_).build();
        return chain.filter(exchange.mutate().request(req.mutate().build()).build());
    }


    @Override
    public int getOrder() {
        return 0;
    }


}
